package site.assad.web;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import site.assad.model.User;
import site.assad.service.UserService;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/10 21:20
 * Description:
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    private static final Logger log = LogManager.getLogger();


    //接收客户端表单信息
    @RequestMapping(value="/handleForm",method= RequestMethod.POST)
    public String handleFrom( @RequestBody String requestBody){
        log.debug(requestBody);
        return "success";
    }

    //向客户端发送一张imageId相关的图片的响应流
    @ResponseBody
    @RequestMapping("/getImg/{imageId}")
    public byte[] getImage(@PathVariable("imageId") String imageId) throws IOException {
        log.debug("imageId: " + imageId);
        Resource res = new ClassPathResource("testimg"+imageId+".jpg");
        byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());
        return fileData;
    }

    //同 handleForm
    @RequestMapping("/handleForm2")
    public String handleForm2( HttpEntity<String> httpEntity){
        log.debug(httpEntity.getHeaders());
        log.debug(httpEntity.getBody());
        return "success";
    }

    //同 getImage
    @RequestMapping("/getImg2/{imageId}")
    public ResponseEntity<byte[]> getImage2(@PathVariable("imageId") String imageId) throws IOException {
        log.debug("imageId: " + imageId);
        Resource res = new ClassPathResource("testimg"+imageId+".jpg");
        byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());

        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(fileData, HttpStatus.OK);

        return responseEntity;
    }

    //输出 User 对象 XML，JSON 格式正文的响应
    @RequestMapping(value="/getUser/{userId}",method=RequestMethod.GET)
    public ResponseEntity<User> handleUser( @PathVariable("userId") int userId){
        User user = userService.getUser(userId);
        ResponseEntity<User> responseEntity = new ResponseEntity<>(user,HttpStatus.OK);

        log.debug(responseEntity.getHeaders());
        log.debug(responseEntity.getBody());
        return responseEntity;
    }

    //从请求中获取 XML 或 JSON 正文，并转化为 User 对象
    @ResponseBody
    @RequestMapping(value="/putUser",method=RequestMethod.POST)
    public String getUser( HttpEntity<User> requestEntity ){
        User user = requestEntity.getBody();
        log.debug(user);
        return "server get User:" + user;
    }



    //演示客户端 AsyncRestTemplate 异步访问服务接口的的服务端接口
    @RequestMapping(value="/getUserAsync",method=RequestMethod.GET)
    public Callable<ResponseEntity<User>> handleUserLongTime(){
        return new Callable<ResponseEntity<User>>() {
            @Override
            public ResponseEntity<User> call() throws Exception {
                User user = new User(1,"assad",12);
                ResponseEntity<User> responseEntity = new ResponseEntity<User>(user,HttpStatus.OK);
                Thread.sleep(1000 * 10); //模拟处理时间
                return responseEntity;
            }
        };
    }










}
